home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / QUERY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-09  |  27.0 KB  |  963 lines

  1. /*****************************************************************************
  2.   
  3.   Zope Public License (ZPL) Version 1.0
  4.   -------------------------------------
  5.   
  6.   Copyright (c) Digital Creations.  All rights reserved.
  7.   
  8.   This license has been certified as Open Source(tm).
  9.   
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions are
  12.   met:
  13.   
  14.   1. Redistributions in source code must retain the above copyright
  15.      notice, this list of conditions, and the following disclaimer.
  16.   
  17.   2. Redistributions in binary form must reproduce the above copyright
  18.      notice, this list of conditions, and the following disclaimer in
  19.      the documentation and/or other materials provided with the
  20.      distribution.
  21.   
  22.   3. Digital Creations requests that attribution be given to Zope
  23.      in any manner possible. Zope includes a "Powered by Zope"
  24.      button that is installed by default. While it is not a license
  25.      violation to remove this button, it is requested that the
  26.      attribution remain. A significant investment has been put
  27.      into Zope, and this effort will continue if the Zope community
  28.      continues to grow. This is one way to assure that growth.
  29.   
  30.   4. All advertising materials and documentation mentioning
  31.      features derived from or use of this software must display
  32.      the following acknowledgement:
  33.   
  34.        "This product includes software developed by Digital Creations
  35.        for use in the Z Object Publishing Environment
  36.        (http://www.zope.org/)."
  37.   
  38.      In the event that the product being advertised includes an
  39.      intact Zope distribution (with copyright and license included)
  40.      then this clause is waived.
  41.   
  42.   5. Names associated with Zope or Digital Creations must not be used to
  43.      endorse or promote products derived from this software without
  44.      prior written permission from Digital Creations.
  45.   
  46.   6. Modified redistributions of any form whatsoever must retain
  47.      the following acknowledgment:
  48.   
  49.        "This product includes software developed by Digital Creations
  50.        for use in the Z Object Publishing Environment
  51.        (http://www.zope.org/)."
  52.   
  53.      Intact (re-)distributions of any official Zope release do not
  54.      require an external acknowledgement.
  55.   
  56.   7. Modifications are encouraged but must be packaged separately as
  57.      patches to official Zope releases.  Distributions that do not
  58.      clearly separate the patches from the original work must be clearly
  59.      labeled as unofficial distributions.  Modifications which do not
  60.      carry the name Zope may be packaged in any form, as long as they
  61.      conform to all of the clauses above.
  62.   
  63.   
  64.   Disclaimer
  65.   
  66.     THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  67.     EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68.     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  69.     PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  70.     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  71.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  72.     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  73.     USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  74.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75.     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  76.     OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  77.     SUCH DAMAGE.
  78.   
  79.   
  80.   This software consists of contributions made by Digital Creations and
  81.   many individuals on behalf of Digital Creations.  Specific
  82.   attributions are listed in the accompanying credits file.
  83.   
  84.  ****************************************************************************/
  85. #include "Python.h"
  86.  
  87. #define UNLESS(E) if(!(E))
  88. #define ASSIGN(V,E) { PyObject *__e; __e = (E); Py_XDECREF(V); (V) = __e; }
  89. #define UNLESS_ASSIGN(V,E) ASSIGN(V, E) if (!(V))
  90.  
  91. static PyTypeObject *RegexType;
  92. static PyObject *string_lower;
  93.  
  94. #define Regex_Check(ob) (ob->ob_type == RegexType)
  95.  
  96. typedef struct
  97. {
  98.     PyObject_HEAD
  99.     PyObject *key;
  100.     PyObject *tests;
  101. } FieldTestobject;
  102.  
  103. staticforward PyTypeObject AttrTesttype, CompAttrTesttype, MethodTesttype;
  104. staticforward PyTypeObject ItemTesttype;
  105.  
  106. typedef struct
  107. {
  108.     PyObject_HEAD
  109.     PyObject *tests;
  110. } Testobject;
  111.  
  112. staticforward PyTypeObject Andtype, Ortype, Cmptype, Regextype, Stringtype;
  113.  
  114. typedef struct
  115. {
  116.     PyObject_HEAD
  117.     PyObject *low;
  118.     PyObject *high;
  119. } Rangeobject;
  120.  
  121. staticforward PyTypeObject Rangetype;
  122.  
  123. static PyObject *reprformat=NULL;
  124.  
  125. static PyObject *
  126. Field_repr(FieldTestobject *self)
  127. {
  128.   PyObject *r;
  129.  
  130.   UNLESS(reprformat) UNLESS(reprformat=PyString_FromString("%s%s")) return NULL;
  131.  
  132.   UNLESS(r=Py_BuildValue("s(OO)", self->ob_type->tp_name,
  133.              self->key, self->tests)) return NULL;
  134.  
  135.   ASSIGN(r,PyString_Format(reprformat,r));
  136.   return r;
  137. }
  138.  
  139. static PyObject *
  140. Test_repr(Testobject *self)
  141. {
  142.   PyObject *r;
  143.  
  144.   UNLESS(reprformat) UNLESS(reprformat=PyString_FromString("%s%s")) return NULL;
  145.  
  146.   UNLESS(r=Py_BuildValue("s(O)", self->ob_type->tp_name, self->tests))
  147.     return NULL;
  148.  
  149.   ASSIGN(r,PyString_Format(reprformat,r));
  150.   return r;
  151. }
  152.  
  153. static PyObject *
  154. Range_repr(Rangeobject *self)
  155. {
  156.   PyObject *r;
  157.  
  158.   UNLESS(reprformat) UNLESS(reprformat=PyString_FromString("%s%s")) return NULL;
  159.  
  160.   UNLESS(r=Py_BuildValue("s(OO)", self->ob_type->tp_name,
  161.              self->low, self->high)) return NULL;
  162.  
  163.   ASSIGN(r,PyString_Format(reprformat,r));
  164.   return r;
  165. }
  166.  
  167.   
  168.  
  169. FieldTestobject *
  170. new_FieldTestobject(PyObject *key, PyObject *tests, PyTypeObject *type)
  171. {
  172.     FieldTestobject *self;
  173.  
  174.     if (!(self = PyObject_NEW(FieldTestobject, type)))
  175.         return NULL;
  176.  
  177.     Py_INCREF(self->key = key);
  178.     Py_INCREF(self->tests = tests);
  179.   
  180.     return self;
  181. }
  182.  
  183. static void
  184. FieldTest_dealloc(FieldTestobject *self)
  185. {
  186.     Py_XDECREF(self->key);
  187.     Py_XDECREF(self->tests);
  188.     PyMem_DEL(self);
  189. }
  190.  
  191. Testobject *
  192. new_Testobject(PyObject *tests, PyTypeObject *type)
  193. {
  194.     Testobject *self;
  195.  
  196.     if (!(self = PyObject_NEW(Testobject, type)))
  197.         return NULL;
  198.  
  199.     Py_INCREF(self->tests = tests);
  200.   
  201.     return self;
  202. }
  203.  
  204. static void
  205. Test_dealloc(Testobject *self)
  206. {
  207.     Py_XDECREF(self->tests);
  208.     PyMem_DEL(self);
  209. }
  210.  
  211. Rangeobject *
  212. new_Rangeobject(PyObject *low, PyObject *high)
  213. {
  214.     Rangeobject *self;
  215.  
  216.     if (!(self = PyObject_NEW(Rangeobject, &Rangetype)))
  217.         return NULL;
  218.  
  219.     Py_INCREF(self->high = high);
  220.     Py_INCREF(self->low = low);
  221.   
  222.     return self;
  223. }
  224.  
  225. static void
  226. Range_dealloc(Rangeobject *self)
  227. {
  228.     Py_XDECREF(self->high);
  229.     Py_XDECREF(self->low);
  230.     PyMem_DEL(self);
  231. }
  232.  
  233. static PyObject *
  234. Query__call__(PyObject *self, PyObject *args, PyObject *kw)
  235. {
  236.   PyObject *arg;
  237.  
  238.   if (!(PyArg_ParseTuple(args, "O", &arg)))
  239.     return NULL;
  240.  
  241.   return PyObject_GetItem(self, arg);
  242. }
  243.  
  244. static PyObject *
  245. AttrTest__getitem__(FieldTestobject *self, PyObject *key)
  246. {
  247.     PyObject *ob;
  248.  
  249.     UNLESS(ob=PyObject_GetAttr(key, self->key)) return NULL;
  250.     ASSIGN(ob, PyObject_GetItem(self->tests, ob));
  251.     return ob;
  252. }
  253.  
  254. static PyObject *
  255. CompAttrTest__getitem__(FieldTestobject *self, PyObject *key)
  256. {
  257.     PyObject *ob;
  258.  
  259.     UNLESS(ob=PyObject_GetAttr(key, self->key)) return NULL;
  260.     UNLESS_ASSIGN(ob, PyObject_CallObject(ob, NULL)) return NULL;
  261.     ASSIGN(ob, PyObject_GetItem(self->tests, ob));
  262.     return ob;
  263. }
  264.  
  265. static PyObject *
  266. MethodTest__getitem__(FieldTestobject *self, PyObject *key)
  267. {
  268.     PyObject *ob;
  269.  
  270.     UNLESS(ob=PyObject_GetAttr(key, self->key)) return NULL;
  271.     ASSIGN(ob,PyObject_CallObject(ob, self->tests));
  272.     return ob;
  273. }
  274.  
  275. static PyObject *
  276. meaningless_len(PyObject *self)
  277. {
  278.   return PyInt_FromLong(1);    /* Use one so we are considered "true" */
  279. }
  280.  
  281. static PyMappingMethods AttrTest_as_mapping = {
  282.     (inquiry)meaningless_len,         /*mp_length*/
  283.     (binaryfunc)AttrTest__getitem__,  /*mp_subscript*/
  284.     (objobjargproc)0,                 /*mp_ass_subscript*/
  285. };
  286.  
  287. static PyMappingMethods CompAttrTest_as_mapping = {
  288.     (inquiry)meaningless_len,         /*mp_length*/
  289.     (binaryfunc)CompAttrTest__getitem__,  /*mp_subscript*/
  290.     (objobjargproc)0,                 /*mp_ass_subscript*/
  291. };
  292.  
  293. static PyMappingMethods MethodTest_as_mapping = {
  294.     (inquiry)meaningless_len,         /*mp_length*/
  295.     (binaryfunc)MethodTest__getitem__,  /*mp_subscript*/
  296.     (objobjargproc)0,                 /*mp_ass_subscript*/
  297. };
  298.  
  299. static PyObject *
  300. AttrTest(PyObject *self, PyObject *args)
  301. {
  302.     PyObject *key, *test;
  303.  
  304.     if (!PyArg_ParseTuple(args, "OO", &key, &test))
  305.         return NULL;
  306.  
  307.     return (PyObject *)new_FieldTestobject(key, test, &AttrTesttype);
  308. }
  309.  
  310. static PyObject *
  311. CompAttrTest(PyObject *self, PyObject *args)
  312. {
  313.     PyObject *key, *test;
  314.  
  315.     if (!PyArg_ParseTuple(args, "OO", &key, &test))
  316.         return NULL;
  317.  
  318.     return (PyObject *)new_FieldTestobject(key, test, &CompAttrTesttype);
  319. }
  320.  
  321. static PyObject *
  322. MethodTest(PyObject *self, PyObject *args)
  323. {
  324.     PyObject *key, *test;
  325.  
  326.     if (!PyArg_ParseTuple(args, "OO", &key, &test))
  327.         return NULL;
  328.  
  329.     UNLESS(PyTuple_Check(test))
  330.       {
  331.     PyErr_SetString(PyExc_TypeError,
  332.             "second argument to MethodTest must be an "
  333.             "argument tuple");
  334.     return NULL;
  335.       }
  336.  
  337.     self=(PyObject *)new_FieldTestobject(key, test, &MethodTesttype);
  338.     return self;
  339. }
  340.  
  341. static PyTypeObject AttrTesttype = {
  342.     PyObject_HEAD_INIT(NULL)
  343.     0,                /*ob_size*/
  344.     "AttrTest",        /*tp_name*/
  345.     sizeof(FieldTestobject),        /*tp_basicsize*/
  346.     0,                /*tp_itemsize*/
  347.     /* methods */
  348.     (destructor)FieldTest_dealloc,    /*tp_dealloc*/
  349.     (printfunc)0,        /*tp_print*/
  350.     (getattrfunc)0,    /*tp_getattr*/
  351.     (setattrfunc)0,    /*tp_setattr*/
  352.     (cmpfunc)0,        /*tp_compare*/
  353.     (reprfunc)Field_repr,        /*tp_repr*/
  354.     0,            /*tp_as_number*/
  355.     0,        /*tp_as_sequence*/
  356.     &AttrTest_as_mapping,        /*tp_as_mapping*/
  357.     (hashfunc)0,        /*tp_hash*/
  358.     (ternaryfunc)Query__call__,        /*tp_call*/
  359.     (reprfunc)0,        /*tp_str*/
  360.  
  361.     /* Space for future expansion */
  362.     0L,0L,0L,0L,
  363.     "Test objects that apply sub-tests to attribute values",
  364. };
  365.  
  366. static PyTypeObject CompAttrTesttype = {
  367.     PyObject_HEAD_INIT(NULL)
  368.     0,                /*ob_size*/
  369.     "CompAttrTest",        /*tp_name*/
  370.     sizeof(FieldTestobject),        /*tp_basicsize*/
  371.     0,                /*tp_itemsize*/
  372.     /* methods */
  373.     (destructor)FieldTest_dealloc,    /*tp_dealloc*/
  374.     (printfunc)0,        /*tp_print*/
  375.     (getattrfunc)0,    /*tp_getattr*/
  376.     (setattrfunc)0,    /*tp_setattr*/
  377.     (cmpfunc)0,        /*tp_compare*/
  378.     (reprfunc)Field_repr,        /*tp_repr*/
  379.     0,            /*tp_as_number*/
  380.     0,        /*tp_as_sequence*/
  381.     &CompAttrTest_as_mapping,        /*tp_as_mapping*/
  382.     (hashfunc)0,        /*tp_hash*/
  383.     (ternaryfunc)Query__call__,        /*tp_call*/
  384.     (reprfunc)0,        /*tp_str*/
  385.  
  386.     /* Space for future expansion */
  387.     0L,0L,0L,0L,
  388.     "Test objects that apply sub-tests to computed attribute values",
  389. };
  390.  
  391. static PyTypeObject MethodTesttype = {
  392.     PyObject_HEAD_INIT(NULL)
  393.     0,                /*ob_size*/
  394.     "MethodTest",        /*tp_name*/
  395.     sizeof(FieldTestobject),        /*tp_basicsize*/
  396.     0,                /*tp_itemsize*/
  397.     /* methods */
  398.     (destructor)FieldTest_dealloc,    /*tp_dealloc*/
  399.     (printfunc)0,        /*tp_print*/
  400.     (getattrfunc)0,    /*tp_getattr*/
  401.     (setattrfunc)0,    /*tp_setattr*/
  402.     (cmpfunc)0,        /*tp_compare*/
  403.     (reprfunc)Field_repr,        /*tp_repr*/
  404.     0,            /*tp_as_number*/
  405.     0,        /*tp_as_sequence*/
  406.     &MethodTest_as_mapping,        /*tp_as_mapping*/
  407.     (hashfunc)0,        /*tp_hash*/
  408.     (ternaryfunc)Query__call__,        /*tp_call*/
  409.     (reprfunc)0,        /*tp_str*/
  410.  
  411.     /* Space for future expansion */
  412.     0L,0L,0L,0L,
  413.     "Test objects that call test methods",
  414. };
  415.  
  416. static PyObject *
  417. ItemTest__getitem__(FieldTestobject *self, PyObject *key)
  418. {
  419.     PyObject *ob;
  420.  
  421.     UNLESS(ob=PyObject_GetItem(key, self->key)) return NULL;
  422.  
  423.     UNLESS_ASSIGN(ob, PyObject_GetItem(self->tests, ob)) return NULL;
  424.  
  425.     return ob;
  426. }
  427.  
  428. static PyMappingMethods ItemTest_as_mapping = {
  429.     (inquiry)meaningless_len,         /*mp_length*/
  430.     (binaryfunc)ItemTest__getitem__,  /*mp_subscript*/
  431.     (objobjargproc)0,                 /*mp_ass_subscript*/
  432. };
  433.  
  434. static PyObject *
  435. ItemTest(PyObject *self, PyObject *args)
  436. {
  437.     PyObject *key, *test;
  438.  
  439.     if (!PyArg_ParseTuple(args, "OO", &key, &test)) return NULL;
  440.  
  441.     return (PyObject *)new_FieldTestobject(key, test, &ItemTesttype);
  442. }
  443.  
  444. static char ItemTesttype__doc__[] = "";
  445.  
  446. static PyTypeObject ItemTesttype = {
  447.     PyObject_HEAD_INIT(NULL)
  448.     0,                /*ob_size*/
  449.     "ItemTest",        /*tp_name*/
  450.     sizeof(FieldTestobject),        /*tp_basicsize*/
  451.     0,                /*tp_itemsize*/
  452.     /* methods */
  453.     (destructor)FieldTest_dealloc,    /*tp_dealloc*/
  454.     (printfunc)0,        /*tp_print*/
  455.     (getattrfunc)0,    /*tp_getattr*/
  456.     (setattrfunc)0,    /*tp_setattr*/
  457.     (cmpfunc)0,        /*tp_compare*/
  458.     (reprfunc)Field_repr,        /*tp_repr*/
  459.     0,            /*tp_as_number*/
  460.     0,        /*tp_as_sequence*/
  461.     &ItemTest_as_mapping,        /*tp_as_mapping*/
  462.     (hashfunc)0,        /*tp_hash*/
  463.     (ternaryfunc)Query__call__,        /*tp_call*/
  464.     (reprfunc)0,        /*tp_str*/
  465.  
  466.     /* Space for future expansion */
  467.     0L,0L,0L,0L,
  468.     ItemTesttype__doc__ /* Documentation string */
  469. };
  470.  
  471. static PyObject *
  472. FieldTest(PyObject *self, PyObject *args)
  473. {
  474.     PyObject *key, *test;
  475.  
  476.     if (!PyArg_ParseTuple(args, "OO", &key, &test))
  477.         return NULL;
  478.  
  479.     return (PyObject *)new_FieldTestobject(key, test, 
  480.         (PyString_Check(key) ? &AttrTesttype : &ItemTesttype));
  481. }
  482.  
  483. static PyObject *
  484. And__getitem__(Testobject *self, PyObject *key)
  485. {
  486.     int len, i;
  487.     PyObject *val=0;
  488.  
  489.     if ((len = PySequence_Length(self->tests)) < 0)
  490.         return NULL;
  491.  
  492.     for (i = 0; i < len; i++)
  493.     {
  494.         UNLESS_ASSIGN(val,PySequence_GetItem(self->tests, i)) return NULL;
  495.  
  496.         UNLESS_ASSIGN(val, PyObject_GetItem(val, key)) return NULL;
  497.  
  498.         if (!PyObject_IsTrue(val)) return val; /* No need to convert to 0 */
  499.     }
  500.  
  501.     if(val) return val;
  502.     else    return PyInt_FromLong(1);
  503. }
  504.  
  505. static PyMappingMethods And_as_mapping = {
  506.     (inquiry)meaningless_len,    /* mp_length        */
  507.     (binaryfunc)And__getitem__,  /* mp_subscript     */
  508.     (objobjargproc)0,            /* mp_ass_subscript */
  509. };
  510.  
  511. static PyObject *
  512. And(PyObject *self, PyObject *args)
  513. {
  514.     return (PyObject *)new_Testobject(args, &Andtype);
  515. }
  516.  
  517. static char Andtype__doc__[] = "";
  518.  
  519. static PyTypeObject Andtype = {
  520.     PyObject_HEAD_INIT(NULL)
  521.     0,                /*ob_size*/
  522.     "And",        /*tp_name*/
  523.     sizeof(Testobject),        /*tp_basicsize*/
  524.     0,                /*tp_itemsize*/
  525.     /* methods */
  526.     (destructor)Test_dealloc,    /*tp_dealloc*/
  527.     (printfunc)0,        /*tp_print*/
  528.     (getattrfunc)0,    /*tp_getattr*/
  529.     (setattrfunc)0,    /*tp_setattr*/
  530.     (cmpfunc)0,        /*tp_compare*/
  531.     (reprfunc)Test_repr,        /*tp_repr*/
  532.     0,            /*tp_as_number*/
  533.     0,        /*tp_as_sequence*/
  534.     &And_as_mapping,        /*tp_as_mapping*/
  535.     (hashfunc)0,        /*tp_hash*/
  536.     (ternaryfunc)Query__call__,        /*tp_call*/
  537.     (reprfunc)0,        /*tp_str*/
  538.  
  539.     /* Space for future expansion */
  540.     0L,0L,0L,0L,
  541.     Andtype__doc__ /* Documentation string */
  542. };
  543.  
  544. static PyObject *
  545. Or__getitem__(Testobject *self, PyObject *key)
  546. {
  547.     int len, i;
  548.     PyObject *val=0;
  549.  
  550.     if ((len = PySequence_Length(self->tests)) < 0)
  551.         return NULL;
  552.  
  553.     for (i = 0; i < len; i++)
  554.     {
  555.         UNLESS_ASSIGN(val, PySequence_GetItem(self->tests, i)) return NULL;
  556.  
  557.         UNLESS_ASSIGN(val, PyObject_GetItem(val, key)) return NULL;
  558.  
  559.         if (PyObject_IsTrue(val)) return val;
  560.     }
  561.  
  562.     if(val) return val;
  563.     else    return PyInt_FromLong(0);
  564. }
  565.  
  566. static PyMappingMethods Or_as_mapping = {
  567.     (inquiry)meaningless_len,   /*mp_length*/
  568.     (binaryfunc)Or__getitem__,  /*mp_subscript*/
  569.     (objobjargproc)0,           /*mp_ass_subscript*/
  570. };
  571.  
  572. static PyObject *
  573. Or(PyObject *self, PyObject *args)
  574. {
  575.     return (PyObject *)new_Testobject(args, &Ortype);
  576. }
  577.  
  578. static char Ortype__doc__[] = "";
  579.  
  580. static PyTypeObject Ortype = {
  581.     PyObject_HEAD_INIT(NULL)
  582.     0,                /*ob_size*/
  583.     "Or",        /*tp_name*/
  584.     sizeof(Testobject),        /*tp_basicsize*/
  585.     0,                /*tp_itemsize*/
  586.     /* methods */
  587.     (destructor)Test_dealloc,    /*tp_dealloc*/
  588.     (printfunc)0,        /*tp_print*/
  589.     (getattrfunc)0,    /*tp_getattr*/
  590.     (setattrfunc)0,    /*tp_setattr*/
  591.     (cmpfunc)0,        /*tp_compare*/
  592.     (reprfunc)Test_repr,        /*tp_repr*/
  593.     0,            /*tp_as_number*/
  594.     0,        /*tp_as_sequence*/
  595.     &Or_as_mapping,        /*tp_as_mapping*/
  596.     (hashfunc)0,        /*tp_hash*/
  597.     (ternaryfunc)Query__call__,        /*tp_call*/
  598.     (reprfunc)0,        /*tp_str*/
  599.  
  600.     /* Space for future expansion */
  601.     0L,0L,0L,0L,
  602.     Ortype__doc__ /* Documentation string */
  603. };
  604.  
  605. static PyObject *
  606. Range__getitem__(Rangeobject *self, PyObject *key)
  607. {
  608.     if ((self->low ==Py_None || PyObject_Compare(key, self->low)  >= 0) &&
  609.         (self->high==Py_None || PyObject_Compare(key, self->high) <= 0))
  610.     {
  611.         return PyInt_FromLong(1);
  612.     }
  613.  
  614.     return PyInt_FromLong(0);
  615. }
  616.  
  617. static PyMappingMethods Range_as_mapping = {
  618.     (inquiry)meaningless_len,      /*mp_length*/
  619.     (binaryfunc)Range__getitem__,  /*mp_subscript*/
  620.     (objobjargproc)0,              /*mp_ass_subscript*/
  621. };
  622.  
  623. static PyObject *
  624. Range(PyObject *self, PyObject *args)
  625. {
  626.     PyObject *high, *low;
  627.  
  628.     if (!PyArg_ParseTuple(args, "OO", &low, &high))
  629.         return NULL;
  630.  
  631.     return (PyObject *)new_Rangeobject(low, high);
  632. }
  633.  
  634. static PyTypeObject Rangetype = {
  635.     PyObject_HEAD_INIT(NULL)
  636.     0,                /*ob_size*/
  637.     "Range",        /*tp_name*/
  638.     sizeof(Rangeobject),        /*tp_basicsize*/
  639.     0,                /*tp_itemsize*/
  640.     /* methods */
  641.     (destructor)Range_dealloc,    /*tp_dealloc*/
  642.     (printfunc)0,        /*tp_print*/
  643.     (getattrfunc)0,    /*tp_getattr*/
  644.     (setattrfunc)0,    /*tp_setattr*/
  645.     (cmpfunc)0,        /*tp_compare*/
  646.     (reprfunc)Range_repr,        /*tp_repr*/
  647.     0,            /*tp_as_number*/
  648.     0,        /*tp_as_sequence*/
  649.     &Range_as_mapping,        /*tp_as_mapping*/
  650.     (hashfunc)0,        /*tp_hash*/
  651.     (ternaryfunc)Query__call__,        /*tp_call*/
  652.     (reprfunc)0,        /*tp_str*/
  653.  
  654.     /* Space for future expansion */
  655.     0L,0L,0L,0L,
  656.     "Range test", /* Documentation string */
  657. };
  658.  
  659. static PyObject *
  660. Cmp__getitem__(Testobject *self, PyObject *key)
  661. {
  662.     if (PyObject_Compare(self->tests, key) == 0)
  663.     {    
  664.         return PyInt_FromLong(1);
  665.     }
  666.  
  667.     return PyInt_FromLong(0);
  668. }
  669.  
  670. static PyMappingMethods Cmp_as_mapping = {
  671.     (inquiry)meaningless_len,    /*mp_length*/
  672.     (binaryfunc)Cmp__getitem__,  /*mp_subscript*/
  673.     (objobjargproc)0,            /*mp_ass_subscript*/
  674. };
  675.  
  676. static PyObject *
  677. Cmp(PyObject *self, PyObject *args)
  678. {
  679.     return (PyObject *)new_Testobject(args, &Cmptype);
  680. }
  681.  
  682. static char Cmptype__doc__[] = "";
  683.  
  684. static PyTypeObject Cmptype = {
  685.     PyObject_HEAD_INIT(NULL)
  686.     0,                /*ob_size*/
  687.     "Cmp",        /*tp_name*/
  688.     sizeof(Testobject),        /*tp_basicsize*/
  689.     0,                /*tp_itemsize*/
  690.     /* methods */
  691.     (destructor)Test_dealloc,    /*tp_dealloc*/
  692.     (printfunc)0,        /*tp_print*/
  693.     (getattrfunc)0,    /*tp_getattr*/
  694.     (setattrfunc)0,    /*tp_setattr*/
  695.     (cmpfunc)0,        /*tp_compare*/
  696.     (reprfunc)Test_repr,        /*tp_repr*/
  697.     0,            /*tp_as_number*/
  698.     0,        /*tp_as_sequence*/
  699.     &Cmp_as_mapping,        /*tp_as_mapping*/
  700.     (hashfunc)0,        /*tp_hash*/
  701.     (ternaryfunc)Query__call__,        /*tp_call*/
  702.     (reprfunc)0,        /*tp_str*/
  703.  
  704.     /* Space for future expansion */
  705.     0L,0L,0L,0L,
  706.     Cmptype__doc__ /* Documentation string */
  707. };
  708.  
  709. static PyObject *
  710. Regex__getitem__(Testobject *self, PyObject *key)
  711. {
  712.     PyObject *t, *search_res, *ret = NULL;
  713.  
  714.     if (!(t = PyTuple_New(1)))
  715.         return NULL;
  716.  
  717.     PyTuple_SET_ITEM(t, 0, key);
  718.     Py_INCREF(key);
  719.  
  720.     if (!(search_res = PyObject_CallObject(self->tests, t)))
  721.         goto finally;
  722.  
  723.     if (!PyInt_Check(search_res))
  724.     {
  725.         PyErr_SetString(PyExc_TypeError, "search method of regular "
  726.             "expression must return an integer");
  727.         goto finally;
  728.     }
  729.  
  730.     if (PyInt_AS_LONG((PyIntObject *)search_res) >= 0)
  731.     {    
  732.         ret = PyInt_FromLong(1);
  733.         goto finally;
  734.     }
  735.  
  736.     ret = PyInt_FromLong(0);
  737.  
  738. finally:
  739.     Py_XDECREF(t);
  740.     Py_XDECREF(search_res);
  741.  
  742.     return ret;
  743. }
  744.  
  745. static PyMappingMethods Regex_as_mapping = {
  746.     (inquiry)meaningless_len,      /*mp_length*/
  747.     (binaryfunc)Regex__getitem__,  /*mp_subscript*/
  748.     (objobjargproc)0,              /*mp_ass_subscript*/
  749. };
  750.  
  751. static PyObject *
  752. Regex(PyObject *self, PyObject *args)
  753. {
  754.     PyObject *search, *r;
  755.  
  756.     static PyObject *search_str = 0;
  757.  
  758.     if (!search_str)
  759.         if (!(search_str = PyString_FromString("search")))
  760.             return NULL;
  761.  
  762.     if (!Regex_Check(args))
  763.     {
  764.         PyErr_SetString(PyExc_TypeError, "argument must be compiled regular"
  765.             " expression");
  766.         return NULL;
  767.     }
  768.  
  769.     if (!(search = PyObject_GetAttr(args, search_str)))
  770.         return NULL;
  771.  
  772.     r = (PyObject *)new_Testobject(search, &Regextype);
  773.  
  774.     Py_DECREF(search);
  775.  
  776.     return r;
  777. }
  778.  
  779. static char Regextype__doc__[] = "";
  780.  
  781. static PyTypeObject Regextype = {
  782.     PyObject_HEAD_INIT(NULL)
  783.     0,                /*ob_size*/
  784.     "Regex",        /*tp_name*/
  785.     sizeof(Testobject),        /*tp_basicsize*/
  786.     0,                /*tp_itemsize*/
  787.     /* methods */
  788.     (destructor)Test_dealloc,    /*tp_dealloc*/
  789.     (printfunc)0,        /*tp_print*/
  790.     (getattrfunc)0,    /*tp_getattr*/
  791.     (setattrfunc)0,    /*tp_setattr*/
  792.     (cmpfunc)0,        /*tp_compare*/
  793.     (reprfunc)Test_repr,        /*tp_repr*/
  794.     0,            /*tp_as_number*/
  795.     0,        /*tp_as_sequence*/
  796.     &Regex_as_mapping,        /*tp_as_mapping*/
  797.     (hashfunc)0,        /*tp_hash*/
  798.     (ternaryfunc)Query__call__,        /*tp_call*/
  799.     (reprfunc)0,        /*tp_str*/
  800.  
  801.     /* Space for future expansion */
  802.     0L,0L,0L,0L,
  803.     Regextype__doc__ /* Documentation string */
  804. };
  805.  
  806. static PyObject *
  807. String__getitem__(Testobject *self, PyObject *key)
  808. {
  809.     char *ckey, *ctests;
  810.     int lkey, ltests, k;
  811.  
  812.     if (!PyString_Check(key))
  813.     {
  814.         PyErr_SetString(PyExc_TypeError, "argument must be string");
  815.         return NULL;
  816.     }
  817.  
  818.     UNLESS(ckey=PyString_AsString(key)) return NULL;
  819.     if((lkey=PyString_Size(key)) < 0) return NULL;
  820.  
  821.     UNLESS(ctests=PyString_AsString(self->tests)) return NULL;
  822.     if((ltests=PyString_Size(self->tests)) < 0) return NULL;
  823.  
  824.     if(lkey != ltests) return PyInt_FromLong(0);
  825.  
  826.     while(lkey--)
  827.       {
  828.     k=*ckey++;
  829.     if(tolower(k) != *ctests++) return PyInt_FromLong(0);
  830.       }
  831.     return PyInt_FromLong(1);
  832. }
  833.  
  834. static PyMappingMethods String_as_mapping = {
  835.     (inquiry)meaningless_len,       /*mp_length*/
  836.     (binaryfunc)String__getitem__,  /*mp_subscript*/
  837.     (objobjargproc)0,               /*mp_ass_subscript*/
  838. };
  839.  
  840. static PyObject *
  841. String(PyObject *self, PyObject *args)
  842. {
  843.   UNLESS(args=PyObject_CallObject(string_lower,args)) return NULL;
  844.   UNLESS_ASSIGN(args,(PyObject *)new_Testobject(args, &Stringtype));
  845.   return args;
  846. }
  847.  
  848. static char Stringtype__doc__[] = "";
  849.  
  850. static PyTypeObject Stringtype = {
  851.     PyObject_HEAD_INIT(NULL)
  852.     0,                /*ob_size*/
  853.     "String",        /*tp_name*/
  854.     sizeof(Testobject),        /*tp_basicsize*/
  855.     0,                /*tp_itemsize*/
  856.     /* methods */
  857.     (destructor)Test_dealloc,    /*tp_dealloc*/
  858.     (printfunc)0,        /*tp_print*/
  859.     (getattrfunc)0,    /*tp_getattr*/
  860.     (setattrfunc)0,    /*tp_setattr*/
  861.     (cmpfunc)0,        /*tp_compare*/
  862.     (reprfunc)Test_repr,        /*tp_repr*/
  863.     0,            /*tp_as_number*/
  864.     0,        /*tp_as_sequence*/
  865.     &String_as_mapping,        /*tp_as_mapping*/
  866.     (hashfunc)0,        /*tp_hash*/
  867.     (ternaryfunc)Query__call__,        /*tp_call*/
  868.     (reprfunc)0,        /*tp_str*/
  869.  
  870.     /* Space for future expansion */
  871.     0L,0L,0L,0L,
  872.     Stringtype__doc__ /* Documentation string */
  873. };
  874.  
  875. static struct PyMethodDef Query_methods[] = {
  876.   {"AttrTest",    (PyCFunction)AttrTest,  1,
  877.    "AttrTest(name,subtest) -- Define a test on an attribute."},
  878.   {"CompAttrTest",    (PyCFunction)CompAttrTest,  1,
  879.    "CompAttrTest(name,subtest) -- Define a test on a computed attribute."},
  880.   {"MethodTest",    (PyCFunction)MethodTest,  1,
  881.    "MethodTest(method,arg) -- Define a test on method and argument.\n\n"
  882.    "Matches occur for those objects for which: o.method(arg)\n"
  883.    "is true."
  884.   },
  885.   {"ItemTest",    (PyCFunction)ItemTest,  1,
  886.    "ItemTest(key,subtest) -- Define a test on an item."},
  887.   {"FieldTest",   (PyCFunction)FieldTest, 1,
  888.    "FieldTest(index,subtest) -- Define a test on an item or attribute.\n"
  889.    "\n"
  890.    "If 'index' is an integer, then an item test will be done, otherwise\n"
  891.    "an attribute test will be done."
  892.   },
  893.   {"And",         (PyCFunction)And,       1,
  894.    "And(*subtests) -- Create a test that is an and of several sub-tests"},
  895.   {"Or",          (PyCFunction)Or,        1,
  896.    "Or(*subtests) -- Create a test that is an or of several sub-tests"},
  897.   {"Range",       (PyCFunction)Range,     1,
  898.    "Range(min,max) -- Create a range test"},
  899.   {"Cmp",         (PyCFunction)Cmp,       0,
  900.    "Cmp(value) -- Define a straight comparison test with the given value"},
  901.   {"Regex",       (PyCFunction)Regex,     0,
  902.    "Regex(compiled_regex) -- Define a Regular-expression test"},
  903.   {"String",      (PyCFunction)String,    1,
  904.    "String(str) -- Define a case-insensitive test"},
  905.   {NULL,        NULL}        /* sentinel */
  906. };
  907.  
  908.  
  909. static char Query_module_documentation[] = 
  910. "Query blocks for composing complex queries without execing Python code\n"
  911. "\n"
  912. "This module provides a number of simple types that can be used to\n"
  913. "compose complex queries that execute in a reasonable effiecient manner\n"
  914. "against objects.  Each object type define objects that can be called\n"
  915. "with a single argument or with 'getitem' to check whether an object,\n"
  916. "such as a database record or a collection item satisfies a query.\n"
  917. "\n$Id: Query.c,v 1.8 1999/03/10 00:15:36 klm Exp $"
  918. ;
  919.  
  920. void
  921. initQuery()
  922. {
  923.     PyObject *m, *d, *regex, *string;
  924.     char *rev="$Revision: 1.8 $";
  925.  
  926.     AttrTesttype.ob_type      =&PyType_Type;
  927.     CompAttrTesttype.ob_type  =&PyType_Type;
  928.     MethodTesttype.ob_type    =&PyType_Type;
  929.     ItemTesttype.ob_type      =&PyType_Type;
  930.     Andtype.ob_type           =&PyType_Type;
  931.     Ortype.ob_type            =&PyType_Type;
  932.     Rangetype.ob_type         =&PyType_Type;
  933.     Cmptype.ob_type           =&PyType_Type;
  934.     Regextype.ob_type         =&PyType_Type;
  935.     Stringtype.ob_type        =&PyType_Type;
  936.  
  937.     UNLESS(regex = PyImport_ImportModule("regex")) return;
  938.     UNLESS_ASSIGN(regex, PyObject_CallMethod(regex, "compile", "s", "a"))
  939.       return;
  940.     RegexType = regex->ob_type;
  941.     Py_DECREF(regex);
  942.  
  943.     UNLESS(string = PyImport_ImportModule("string")) return;
  944.  
  945.     UNLESS(string_lower = PyObject_GetAttrString(string, "lower")) return;
  946.     Py_DECREF(string);
  947.  
  948.     /* Create the module and add the functions */
  949.     m = Py_InitModule4("Query", Query_methods,
  950.              Query_module_documentation,
  951.              (PyObject*)NULL,PYTHON_API_VERSION);
  952.  
  953.     /* Add some symbolic constants to the module */
  954.     d = PyModule_GetDict(m);
  955.     PyDict_SetItemString(d, "__version__",
  956.              PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
  957.     
  958.   /* Check for errors */
  959.   if (PyErr_Occurred())
  960.     Py_FatalError("can't initialize module BTree");
  961.  
  962. }
  963.